package com.yhp.aop;

import org.apache.tomcat.jni.Proc;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Aspect
public class ServiceAop {

    final  static Logger logger= LoggerFactory.getLogger(ServiceAop.class);

    // .. 表示当前包及其子包
    //用来计算service层某个方法的执行时间
    @Around("execution(* com.yhp.service.impl..*.*(..))")
    public Object huanrao(ProceedingJoinPoint joinPoint){

        logger.info("===开始{}.{}===",joinPoint.getTarget().getClass(),joinPoint.getSignature().getName());
        long begin = System.currentTimeMillis();
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        long end = System.currentTimeMillis();
        long time=end-begin;
        //设置错误级别信息
        if(time>3000){
            logger.error("====执行时间:{}毫秒====",time);
        }else if(time>2000){
            logger.warn("====执行时间:{}毫秒====",time);
        }else{
            logger.info("====执行时间:{}毫秒====",time);
        }
        return proceed;

    }

}
